package pandora.apps.d3mob.view;

import java.util.ArrayList;

import android.graphics.Rect;
import com.e3roid.drawable.controls.StickController;
import com.e3roid.drawable.sprite.AnimatedSprite;
import com.e3roid.drawable.texture.TiledTexture;

public class King extends AnimatedSprite {
	
	public static final int KING_ANIM_SPEED = 250;
	private ArrayList<AnimatedSprite.Frame> downFrames  = new ArrayList<AnimatedSprite.Frame>();
	private ArrayList<AnimatedSprite.Frame> upFrames    = new ArrayList<AnimatedSprite.Frame>();
	private ArrayList<AnimatedSprite.Frame> leftFrames  = new ArrayList<AnimatedSprite.Frame>();
	private ArrayList<AnimatedSprite.Frame> rightFrames = new ArrayList<AnimatedSprite.Frame>();
	
	private int lastDir = -1;
	public boolean run = true;
	public static final int CONTROLLER_SENSITIVITY = 40;
	
	
	public King(TiledTexture texture, int centerX, int centerY) {
		super(texture,centerX,centerY);
		// Initialize animation frames from tile.
		downFrames = new ArrayList<AnimatedSprite.Frame>();
		downFrames.add(new AnimatedSprite.Frame(0, 0));
		downFrames.add(new AnimatedSprite.Frame(1, 0));
		downFrames.add(new AnimatedSprite.Frame(2, 0));
		downFrames.add(new AnimatedSprite.Frame(3, 0));
		
		leftFrames = new ArrayList<AnimatedSprite.Frame>();
		leftFrames.add(new AnimatedSprite.Frame(0, 1));
		leftFrames.add(new AnimatedSprite.Frame(1, 1));
		leftFrames.add(new AnimatedSprite.Frame(2, 1));
		leftFrames.add(new AnimatedSprite.Frame(3, 1));

		rightFrames = new ArrayList<AnimatedSprite.Frame>();
		rightFrames.add(new AnimatedSprite.Frame(0, 2));
		rightFrames.add(new AnimatedSprite.Frame(1, 2));
		rightFrames.add(new AnimatedSprite.Frame(2, 2));
		rightFrames.add(new AnimatedSprite.Frame(3, 2));

		upFrames = new ArrayList<AnimatedSprite.Frame>();
		upFrames.add(new AnimatedSprite.Frame(0, 3));
		upFrames.add(new AnimatedSprite.Frame(1, 3));
		upFrames.add(new AnimatedSprite.Frame(2, 3));
		upFrames.add(new AnimatedSprite.Frame(3, 3));
	}
	
	@Override
	public Rect getCollisionRect() {
		// king's collision rectangle is just around his body.
		Rect rect = this.getRect();
		rect.left   = rect.left   + this.getWidth() / 3;
		rect.right  = rect.right  - this.getWidth() / 3;
		rect.top    = rect.top    + this.getHeight() / 3;
		rect.bottom = rect.bottom - this.getHeight() / 3;
		return rect;
	}
	
	public void animate(int stick_dir) {
		if ((this.lastDir == -1) || (stick_dir != this.lastDir)) {
			this.lastDir = stick_dir;
			if (stick_dir == StickController.UP) {
				animate(King.KING_ANIM_SPEED, upFrames);
			} else if (stick_dir == StickController.DOWN) {
				animate(King.KING_ANIM_SPEED, downFrames);
			} else if (stick_dir == StickController.LEFT) {
				animate(King.KING_ANIM_SPEED, leftFrames);
			} else if (stick_dir == StickController.RIGHT) {
				animate(King.KING_ANIM_SPEED, rightFrames);
			}
		}
	}
}